const {readJson} = require('../../until/fs');
const getCookies = require('../../until/getCookies');
const {SHOP_JSON,STORE_UID} = require('../../config/config.data');
const uuid        = require('node-uuid')
// 小程序商家登录
let storeloginIn = (req,res)=>{
    const {shopUser,shopPwd} = req.body

    let shopData = readJson(SHOP_JSON)
    let resultData = shopData.find(item=>item.shopUser === shopUser)

    if(!resultData){
        res.send({code:0,msg:'没有该账号,请先与健康宝平台签署合作'})
        return
    }
    if(resultData.shopPwd !== shopPwd){
        res.send({code:0,msg:'该账号密码错误'})
        return
    }

     //基于时间戳 生成随机字符串 v4随机字符串
     let storeuid = uuid.v1()

     // 在服务端缓存一个用户状态
     STORE_UID.push({storeuid})
  
    res.send({code:1,msg:'登录成功',data:{shopID:resultData.shopID,storeuid}})

}
let storeloginOut = (req,res )=>{
     // 获取客户端uid
     let storeuid = getCookies(req.get('cookie'),'storeuid');

     // 通过uid从缓存中筛查缓存用户状态
     let index = STORE_UID.findIndex(item=>item.storeuid === storeuid);
 
     // 对缓存用户状态进行清除
     if(index>-1){
            STORE_UID.splice(index,1)
     }
 
     // 清除客户端的cookie中的uid
     res.clearCookie('storeuid')
     
     res.send({code:1,msg:'退出成功',data:[]})
}

module.exports={storeloginIn,storeloginOut}